import {Component, Input, NgModule, OnInit} from '@angular/core';
import {LunhuiWebFrontMapSceneComponent} from '../lunhui-web-front-map-scene/lunhui-web-front-map-scene.component';
import {LunhuiWebFrontMapMonsterComponent} from '../lunhui-web-front-map-monster/lunhui-web-front-map-monster.component';
import {MapInfo} from '../lunhui-web-front-dto/MapInfo';
import {HttpUtil} from '../lunhui-web-front-utils/HttpUtil';
import {HttpQueryParamsDto} from '../lunhui-web-front-dto/HttpQueryParamsDto';
import {HttpMethodEnum} from '../lunhui-web-front-utils/HttpMethodEnum';
import {environment} from '../../environments/environment';
import {ResponseBaseDto} from '../lunhui-web-front-dto/ResponseBaseDto';
import {Monster} from '../lunhui-web-front-dto/Monster';

import * as $ from 'jquery';
import {BrowserUtils} from '../lunhui-web-front-utils/BrowserUtils';

@Component({
    selector: 'app-lunhui-web-front-map',
    templateUrl: './lunhui-web-front-map.component.html',
    styleUrls: ['./lunhui-web-front-map.component.scss']
})

@NgModule({
    declarations: [
        LunhuiWebFrontMapSceneComponent,
        LunhuiWebFrontMapMonsterComponent
    ]
})

export class LunhuiWebFrontMapComponent implements OnInit {

    mapSceneInfo: Array<MapInfo> = null;
    monsterMapInfo: Array<Monster> = null;

    constructor() {
    }

    ngOnInit() {
        const result: boolean = BrowserUtils.checkToken();

        if (!result) {
            BrowserUtils.backToTarget('login');
            return;
        }

        this.getMapSceneInfo();
        $('#navBtnFirst').addClass('mapNavSelected');
    }

    // 获取当前场景位置数据
    public async getMapSceneInfo(event: MouseEvent = null): Promise<void> {

        this.clearCache();

        if (event != null && event.target !== null) {
            this.resetNavBtnStyle(event.target);
        }

        const httpQueryParamsDto: HttpQueryParamsDto = new HttpQueryParamsDto();
        httpQueryParamsDto.url = `${environment.host}map/sceneMapInfo?areaId=0&sceneId=0`;
        httpQueryParamsDto.method = HttpMethodEnum.GET;

        const resultData = await HttpUtil.query(httpQueryParamsDto) as ResponseBaseDto;

        if (resultData.success && resultData.result !== null) {
            this.mapSceneInfo = resultData.result.list;
        } else {
            HttpUtil.queryError(resultData.errorMessage);
        }
    }

    // 获取当前场景怪物分布数据
    public async getMapMonsterInfo(event: MouseEvent = null): Promise<void> {

        this.clearCache();

        if (event != null && event.target !== null) {
            this.resetNavBtnStyle(event.target);
        }

        const httpQueryParamsDto: HttpQueryParamsDto = new HttpQueryParamsDto();
        httpQueryParamsDto.url = `${environment.host}map/monsterMapInfo?areaId=0&sceneId=0`;
        httpQueryParamsDto.method = HttpMethodEnum.GET;

        const resultData = await HttpUtil.query(httpQueryParamsDto) as ResponseBaseDto;

        if (resultData.success && resultData.result !== null) {
            const resultObj = JSON.parse(resultData.result);
            this.monsterMapInfo = resultObj.list;
        } else {
            HttpUtil.queryError(resultData.errorMessage);
        }
    }

    private clearCache(): void {
        this.mapSceneInfo = null;
        this.monsterMapInfo = null;
    }

    // 重置选项卡点击样式
    private resetNavBtnStyle(target: EventTarget): void {
        $(target).addClass('mapNavSelected')
            .siblings().removeClass('mapNavSelected');
    }

    private backHome(): void {
        const origin = location.origin;
        const pathname = location.pathname;

        location.href = `${origin}${pathname}#/home`;
    }

}
